﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.Entity;
using ClassLibrary.DAO;
using ClassLibrary.Data;
public partial class Admin_UserControl_ucListItems : System.Web.UI.UserControl
{
    TblItemsDAO ItemDAO = new TblItemsDAO(Global.CONNECTION_STRING);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }

    }

    private void LoadData()
    {
        grdItems.DataSource = ItemDAO.GetItemsAll();
        grdItems.DataBind();
    }
    protected void grdItems_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdItems.PageIndex = e.NewPageIndex;
        LoadData();
    }
    protected void grdItems_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            
            HiddenField hidItemID = (HiddenField)e.Row.Cells[1].FindControl("hidItemID");
            hidItemID.Value = DataBinder.Eval(e.Row.DataItem, "ItemID").ToString();
        }
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow grd in grdItems.Rows)
        {
            CheckBox ckDel = (CheckBox)grd.FindControl("ckDel");
            bool delete = false;
            try
            {
                if (ckDel.Checked == true)
                {
                    HiddenField hidItemID = (HiddenField)grd.FindControl("hidItemID");
                    string ItemID = hidItemID.Value;
                    ItemDAO.DeleteItem(Convert.ToInt32(ItemID));
                }
                delete = true;
            }
            catch (Exception ex)
            {
                Session[Global.SESSION_ERROR] = ex;
                Response.Redirect(Global.ERROR_PAGE_ADMIN);
            }
        }
        LoadData();
        
    }
}
